package com.xjgzinfo.usdp.service;

import com.xjgzinfo.usdp.domain.BladeVisualCategory;
import com.xjgzinfo.usdp.domain.BladeVisualCategory_;
import com.xjgzinfo.usdp.jpa.service.QueryService;
import com.xjgzinfo.usdp.repository.BladeVisualCategoryRepository;
import com.xjgzinfo.usdp.service.dto.BladeVisualCategoryCriteria;
import com.xjgzinfo.usdp.service.dto.BladeVisualCategoryDTO;
import com.xjgzinfo.usdp.service.mapper.BladeVisualCategoryMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service for executing complex queries for {@link BladeVisualCategory} entities in the database.
 * The main input is a {@link BladeVisualCategoryCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link BladeVisualCategoryDTO} or a {@link Page} of {@link BladeVisualCategoryDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class BladeVisualCategoryQueryService extends QueryService<BladeVisualCategory> {

    private final Logger log = LoggerFactory.getLogger(BladeVisualCategoryQueryService.class);

    private final BladeVisualCategoryRepository bladeVisualCategoryRepository;

    private final BladeVisualCategoryMapper bladeVisualCategoryMapper;

    public BladeVisualCategoryQueryService(BladeVisualCategoryRepository bladeVisualCategoryRepository, BladeVisualCategoryMapper bladeVisualCategoryMapper) {
        this.bladeVisualCategoryRepository = bladeVisualCategoryRepository;
        this.bladeVisualCategoryMapper = bladeVisualCategoryMapper;
    }

    /**
     * Return a {@link List} of {@link BladeVisualCategoryDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<BladeVisualCategoryDTO> findByCriteria(BladeVisualCategoryCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<BladeVisualCategory> specification = createSpecification(criteria);
        return bladeVisualCategoryMapper.toDto(bladeVisualCategoryRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link BladeVisualCategoryDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page     The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<BladeVisualCategoryDTO> findByCriteria(BladeVisualCategoryCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<BladeVisualCategory> specification = createSpecification(criteria);
        return bladeVisualCategoryRepository.findAll(specification, page)
                .map(bladeVisualCategoryMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(BladeVisualCategoryCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<BladeVisualCategory> specification = createSpecification(criteria);
        return bladeVisualCategoryRepository.count(specification);
    }

    /**
     * Function to convert {@link BladeVisualCategoryCriteria} to a {@link Specification}
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<BladeVisualCategory> createSpecification(BladeVisualCategoryCriteria criteria) {
        Specification<BladeVisualCategory> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getId(), BladeVisualCategory_.id));
            }
            if (criteria.getCategoryKey() != null) {
                specification = specification.and(buildStringSpecification(criteria.getCategoryKey(), BladeVisualCategory_.categoryKey));
            }
            if (criteria.getCategoryValue() != null) {
                specification = specification.and(buildStringSpecification(criteria.getCategoryValue(), BladeVisualCategory_.categoryValue));
            }
            if (criteria.getIsDeleted() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsDeleted(), BladeVisualCategory_.isDeleted));
            }
        }
        return specification;
    }
}